/**
 * create the root namespace and making sure we're not overwriting it
 */
var TreToday = TreToday || {};

/**
 * create a general purpose namespace method
 * this will allow us to create namespace a bit easier
 * @param namespace name
 * @returns {*|{}}
 */
TreToday.createNS = function (namespace) {
    var nsparts = namespace.split(".");
    var parent = TreToday;

    // we want to be able to include or exclude the root namespace
    // So we strip it if it's in the namespace
    if (nsparts[0] === "TreToday") {
        nsparts = nsparts.slice(1);
    }

    // loop through the parts and create
    // a nested namespace if necessary
    for (var i = 0; i < nsparts.length; i++) {
        var partname = nsparts[i];
        // check if the current parent already has
        // the namespace declared, if not create it
        if (typeof parent[partname] === "undefined") {
            parent[partname] = {};
        }
        // get a reference to the deepest element
        // in the hierarchy so far
        parent = parent[partname];
    }
    // the parent is now completely constructed
    // with empty namespaces and can be used.
    return parent;
};

/**
 * Root web url: bookstoop.com.
 * @type {boolean}
 */
TreToday.baseUrl = false;

/**
 * Common helper functions
 */
TreToday.createNS('TreToday.Helpers');
TreToday.Helpers.alertMessage = function (o) {
    var _default = {
        container: "#container",
        message: "message",
        type: "success"
    }
    var opts = $.extend(_default, o);
    var $container = $(opts.container);
    switch (opts.type) {
        case "success":
        {
            var html =
                '<div class="alert alert-success alert-dismissable">' +
                    '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' +
                    opts.message +
                    '</div>';
            $container.html(html);
            break;
        }
    }
};

TreToday.Helpers.displayErrors = function (errors, modelName) {
    if (errors) {
        $.each(errors, function (key, value) {
            $("#" + modelName + "_" + key + "_em_").show().html(value);
            $("#" + modelName + "_" + key).addClass("has-error");
        });
    }
};

TreToday.Helpers.block = function ($wrapper) {
    if (typeof $wrapper === "string") {
        $wrapper = $($wrapper);
    }
    var opts = {
        overlayCSS: {
            opacity: 0
        },
        message: '<img src="' + TreToday.baseUrl + '/images/loading.gif" alt="Loading..." title="Loading..." />'
    };
    if ($wrapper) {
        var tagName = $wrapper.prop("tagName");
        switch (tagName) {
            case "TD":
                $wrapper.blockCell(opts);
                break;
            case "TR":
                $wrapper.blockRow(opts);
                break;
            default:
                $wrapper.block(opts);
                break;
        }
    } else {
        $.blockUI(opts);
    }
};
TreToday.Helpers.unblock = function ($wrapper) {
    if (typeof $wrapper === "string") {
        $wrapper = $($wrapper);
    }
    if ($wrapper) {
        var tagName = $wrapper.prop("tagName");
        switch (tagName) {
            case "TD":
                $wrapper.unblockCell();
                break;
            case "TR":
                $wrapper.unblockRow();
                break;
            default:
                $wrapper.unblock();
                break;
        }
    } else {
        $.unblockUI();
    }
};

